#include "window.h"

#include <QFileDialog>

namespace Ui {
	
Window::Window(void) {	
	glwidget = new Ui::GLWidget(this);
	setCentralWidget(glwidget);
	statusbar = new QStatusBar(this);
	setStatusBar(statusbar);
	
	menubar = new QMenuBar();
	QMenu* menu = new QMenu();
	menu->setTitle(tr("File"));
	
	open = new QAction(this);
	open->setText(tr("Open"));
	open->setToolTip(tr("Open a scene"));
	open->setStatusTip(tr("Open a scene."));
	open->setShortcut(tr("Ctrl+O"));
	
	connect(open,SIGNAL(triggered()),this,SLOT(open_file()));
	
	smooth_shade_model = new QAction(this);
	smooth_shade_model->setText(tr("Smooth Shade Model"));
	smooth_shade_model->setShortcut(tr("Ctrl+M"));
	
	connect(smooth_shade_model,SIGNAL(triggered()),this,SLOT(change_shade_model()));
	
	exit = new QAction(this);
	exit->setText(tr("Exit"));
	exit->setStatusTip(tr("Exit"));
	
	connect(exit,SIGNAL(triggered()),this,SLOT(close()));
	
	
	radiosity_panel = new Radiosity_Panel(this);
	dock_radiosity_panel = new QDockWidget(tr("Radiosity Panel"),this);
	dock_radiosity_panel->setWidget(radiosity_panel);
	addDockWidget(Qt::LeftDockWidgetArea,dock_radiosity_panel);
	QAction* view_radiosity_action = dock_radiosity_panel->toggleViewAction();

	connect(radiosity_panel,SIGNAL(radiosity_done()),this,SLOT(update_glwidget()));

	menu->addAction(open);
	menu->addAction(smooth_shade_model);
	menu->addAction(view_radiosity_action);
	menu->addAction(exit);
	
	menubar->addMenu(menu);
	
	setMenuBar(menubar);
}

Window::~Window(void) {	
	if(glwidget != 0) delete glwidget;
	if(statusbar != 0) delete statusbar;
}
	
void Window::open_file(void) {
	QString filename = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath(),tr("t2c2 Files (*.t2c2)"));
	t2c2::Radiosity* radiosity = glwidget->load_file(filename.toStdString().c_str());
	radiosity_panel->set_radiosity(radiosity);
	
}

void Window::change_shade_model(void) {
	glwidget->set_shade_model(!glwidget->get_shade_model());
	glwidget->updateGL();
}

void Window::update_glwidget(void) {
	glwidget->scene_opengl();
	glwidget->updateGL();
}
	
}

